/**
 * Copyright (C) 2018-2022
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package co.yixiang.yshop.module.product.controller.app.product;


import cn.hutool.core.util.ObjectUtil;
import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.module.product.controller.admin.storeproduct.vo.CanvasProductPageReqVO;
import co.yixiang.yshop.module.product.controller.admin.storeproduct.vo.StoreProductRespVO;
import co.yixiang.yshop.module.product.controller.app.product.param.AppStoreProductQueryParam;
import co.yixiang.yshop.module.product.controller.app.product.vo.*;
import co.yixiang.yshop.module.product.enums.product.ProductEnum;
import co.yixiang.yshop.module.product.service.storeproduct.AppStoreProductService;
import co.yixiang.yshop.module.product.service.storeproduct.StoreProductService;
import co.yixiang.yshop.module.product.service.storeproductreply.AppStoreProductReplyService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Set;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;
import static co.yixiang.yshop.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * <p>
 * 商品控制器
 * </p>
 *
 * @author hupeng
 * @since 2023-6-12
 */
@Slf4j
@RestController
@Tag(name = "用户 APP - 商品")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/product")
public class AppStoreProductController {

    private final AppStoreProductService storeProductService;
    private final AppStoreProductReplyService appStoreProductReplyService;
    private final StoreProductService adminProductService;


    /**
     * 获取首页产品
     */
    @GetMapping("/shop/index")
    @Operation(summary = "获取首页信息")
    public CommonResult<AppIndexVo> index() {
        AppIndexVo indexVo = AppIndexVo.builder()
                .firstList(storeProductService.getList(1, 3, ProductEnum.TYPE_3.getValue()))
                .likeInfo(storeProductService.getList(1, 3, ProductEnum.TYPE_2.getValue()))
                .build();
        return success(indexVo);
    }


    /**
     * 获取产品列表
     */
    @GetMapping("/products")
    @Operation(summary = "商品列表")
    public CommonResult<List<AppStoreProductRespVo>> goodsList(AppStoreProductQueryParam productQueryParam) {
        return success(storeProductService.getGoodsList(productQueryParam));
    }


    /**
     * 为你推荐
     */
    @GetMapping("/hot")
    @Operation(summary = "为你推荐")
    public CommonResult<List<AppStoreProductRespVo>> productRecommend() {
        return success(storeProductService.getList(1, 4, ProductEnum.TYPE_2.getValue()));
    }


    /**
     * 普通商品详情
     */
    @GetMapping("/detail")
    @Operation(summary = "普通商品详情")
    @Parameter(name = "id", description = "商品ID", required = true, example = "1024")
    public CommonResult<AppProductVo> detail(@Valid AppStoreProductDetailReqVo vo) {
        long uid = 0;
        System.out.println("uid:" + getLoginUserId());
        if (ObjectUtil.isNotNull(getLoginUserId())) {
            uid = getLoginUserId();
        }
        AppProductVo productVo = storeProductService.goodsDetail(vo, uid);
        return success(productVo);
    }


    /**
     * 获取产品评论
     */
    @GetMapping("/reply/list/{id}")
    @Parameters({
            @Parameter(name = "id", description = "商品ID",
                    required = true, example = "1"),
            @Parameter(name = "type", description = "评论分数类型 0-全部 1-好评 2-中评 3-差评 4-有图",
                    required = true, example = "1"),
            @Parameter(name = "page", description = "页码,默认为1",
                    required = true, example = "1"),
            @Parameter(name = "limit", description = "页大小,默认为10",
                    required = true, example = "10")
    })
    @Operation(summary = "获取产品评论")
    public CommonResult<Page<AppStoreProductReplyQueryVo>> replyList(@PathVariable Long id,
                                                                     @RequestParam(value = "type", defaultValue = "0") int type,
                                                                     @RequestParam(value = "page", defaultValue = "1") int page,
                                                                     @RequestParam(value = "limit", defaultValue = "10") int limit) {
        return success(appStoreProductReplyService.getReplyList(id, type, page, limit));
    }

    /**
     * 获取产品评论数据
     */
    @GetMapping("/reply/config/{id}")
    @Parameter(name = "id", description = "商品ID", required = true, example = "1")
    @Operation(summary = "获取产品评论数据")
    public CommonResult<AppReplyCountVo> replyCount(@PathVariable Integer id) {
        return success(appStoreProductReplyService.getReplyCount(id));
    }


    /**
     * 商品历史搜索记录
     */
    @GetMapping("/search/history")
    @Operation(summary = "商品历史搜索记录")
    public CommonResult<Set<String>> searchHistory() {
        long uid = 0;
        if (ObjectUtil.isNotNull(getLoginUserId())) {
            uid = getLoginUserId();
        }
        return success(storeProductService.listRecentSearch(uid));
    }

    /**
     * 清除商品历史搜索记录
     */
    @DeleteMapping("/search/history/del")
    @Operation(summary = "清除商品历史搜索记录")
    public CommonResult<Boolean> delSearchHistory() {
        long uid = 0;
        if (ObjectUtil.isNotNull(getLoginUserId())) {
            uid = getLoginUserId();
        }
        storeProductService.removeSearchHistoryAll(uid);
        return success(true);
    }

    /**
     * 热门搜索
     */
    @GetMapping("/search/popular/{start}/{end}")
    @Operation(summary = "热门搜索")
    public CommonResult<Set<String>> searchPopular(@PathVariable Long start, @PathVariable Long end) {
        return success(storeProductService.searchPopular(start, end));
    }

    @GetMapping("/canvas/page")
    @Operation(summary = "获得画布商品分页")
    public CommonResult<PageResult<StoreProductRespVO>> getCanvasProductPage(@Valid CanvasProductPageReqVO pageVO) {
        return success(adminProductService.getCanvasProductPage(pageVO));
    }


}

